<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Bitrate calculation with CANtegrity</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_example_c_kvdiag_autobaud.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Bitrate calculation with CANtegrity </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>samples\example\c\kvdiag\autobaud.c</b> </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;stdio.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;signal.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="kv_diag_8h.html">kvDiag.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status); <span class="comment">/* Checks for errors */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> intHandler(<span class="keywordtype">int</span> unused); <span class="comment">/* Handle Ctrl-C */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage(); <span class="comment">/* Print usage */</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> interrupt = 0;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> channel = -1;</div><div class="line"></div><div class="line">  <span class="comment">/* Parse command-line parameters. */</span></div><div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=1; i&lt;argc; i++) {</div><div class="line">    <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-ch=%d&quot;</span>, &amp;channel) == 1);</div><div class="line">    <span class="keywordflow">else</span> {</div><div class="line">      usage();</div><div class="line">      exit(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (channel == -1) {</div><div class="line">    usage();</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">  <span class="comment">/* We open the channel with the EXCLUSIVE flag to not let any other</span></div><div class="line"><span class="comment">   * application interfere with the CANtegrity channel */</span></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line"></div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <span class="comment">/* Attach an analyzer to an open CAN channel. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga76d09ccb89cf756d954144582f730330">kvDiagAttachAnalyzer</a>(hnd, <a class="code" href="kv_diag_8h.html#a29188edd320816f6f3b4e98e6b331a66">DIAG_ANALYZER_TYPE_DEFAULT</a>);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagAttachAnalyzer&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* We want to run DIAG_PROGRAM_TYPE_AUTOBAUD. It doesn&#39;t need a configuration,</span></div><div class="line"><span class="comment">   * so we pass an empty JSON string. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#gad615ea3768542e4d48f10d0dca4ef46d">kvDiagSetProgram</a>(hnd, <a class="code" href="kv_diag_8h.html#a64bb36d4e56a896eb2af5dc09cce1542">DIAG_PROGRAM_TYPE_AUTOBAUD</a>, <span class="stringliteral">&quot;{}&quot;</span>);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagSetProgram&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Start the analyzer. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga9cb62a80b9ffe4ea80b22acfb49bd3a4">kvDiagStart</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagStart&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Continuously measure bitrate until Ctrl-C is pressed. */</span></div><div class="line">  signal(SIGINT, intHandler);</div><div class="line"></div><div class="line">  <a class="code" href="structbitrates__t.html">bitrates_t</a> btrs;</div><div class="line">  <span class="keywordflow">while</span> (1) {</div><div class="line">    Sleep(100); <span class="comment">/* Wait for samples to accumulate. */</span></div><div class="line">    stat = <a class="code" href="group__kvdiag__diagnostics.html#gabc77cf695c7fb518263d32539a3aec9e">kvDiagCalculateBitrate</a>(hnd, &amp;btrs);</div><div class="line">    <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      printf(<span class="stringliteral">&quot;Estimated CAN bitrate to %.0f KBit/s with quality %d %%.\n&quot;</span>,</div><div class="line">             btrs.<a class="code" href="structbitrates__t.html#a9b69ac1d9a035cf32487c02eb1e90368">bitrate</a>.<a class="code" href="structbitrate__t.html#a8cbd72632a1cb167e5ca769b067cfcec">bitrate</a>, btrs.<a class="code" href="structbitrates__t.html#a9b69ac1d9a035cf32487c02eb1e90368">bitrate</a>.<a class="code" href="structbitrate__t.html#acac7df77df55f2a1bfd8d8d18340b773">quality</a>);</div><div class="line">    }</div><div class="line">    <span class="comment">/* Reset bitrate calculation to get a fresh measurement. */</span></div><div class="line">    stat = <a class="code" href="group__kvdiag__diagnostics.html#ga65bbdc1e8c8bf4affc8ba2041d72ff1a">kvDiagResetBitrateCalculation</a>(hnd);</div><div class="line">    check(<span class="stringliteral">&quot;kvDiagResetBitrateCalculation&quot;</span>, stat);</div><div class="line"></div><div class="line">    <span class="comment">/* Check Ctrl-C. */</span></div><div class="line">    <span class="keywordflow">if</span> (interrupt) {</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">/* Stop the analyzer. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga685c95a3e168601a28e680d9c820dd8a">kvDiagStop</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagStop&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Detach the analyzer, since we&#39;re not going to restart it. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga61263295b3feaad95c257eef05a9579b">kvDiagDetachAnalyzer</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagDetachAnalyzer&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Close the CAN channel. */</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;canClose&quot;</span>, stat);</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (status != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    printf(<span class="stringliteral">&quot;%s returned %d\n&quot;</span>, name, status);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> intHandler(<span class="keywordtype">int</span> unused)</div><div class="line">{</div><div class="line">  interrupt = 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage()</div><div class="line">{</div><div class="line">  printf(</div><div class="line">    <span class="stringliteral">&quot;Usage: &#39;autobaud_example -ch=n&#39;, &quot;</span></div><div class="line">    <span class="stringliteral">&quot;where &#39;n&#39; is a CANlib channel with analyzer capability.\n\n&quot;</span></div><div class="line">    <span class="stringliteral">&quot;Note: This program requires a working CAN bus with traffic to function properly.\n&quot;</span>);</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li><li class="navelem"><a class="el" href="page_user_guide_canlib_samples.html">Sample Programs (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
